package com.example.gifframecpp

import android.content.Context
import android.os.Handler
import android.os.Looper
import java.io.File
import java.util.concurrent.Executors

class GifCopyManager(private val context: Context) {
    val NAME_GIF="demo.gif"

    private val executor = Executors.newSingleThreadExecutor()
    private val handler = Handler(Looper.getMainLooper())

    fun getGifFilePath(callback: (String?) -> Unit) {
        executor.execute {
            val cacheDir = context.cacheDir
            val gifFile = File(cacheDir, NAME_GIF)

            // 删除已有的缓存文件
            if (gifFile.exists()) {
                gifFile.delete()
            }

            try {
                val assetManager = context.assets
                val gifInputStream = assetManager.open(NAME_GIF)
                gifInputStream.use { input ->
                    gifFile.outputStream().use { output ->
                        input.copyTo(output)
                    }
                }

                val gifFilePath = gifFile.absolutePath
                handler.post {
                    callback(gifFilePath)
                }
            } catch (e: Exception) {
                handler.post {
                    callback(null)
                }
            }
        }
    }
}